<template>
  <div class="login-page">
    <div class="login-left">
      <div class="left-inner">
        <div class="hot-badge">热门拍卖平台</div>
        <h2 class="welcome">欢迎回来</h2>
        <p class="desc">登录以查看北交 河北等网站的拍卖数据。</p>
        <div class="feature">
          <i class="el-icon-lightning"></i>
          <span>实时热点追踪</span>
        </div>
        <div class="feature">
          <i class="el-icon-data-analysis"></i>
          <span>趋势可视化</span>
        </div>
        <div class="feature">
          <i class="el-icon-s-check"></i>
          <span>安全可靠</span>
        </div>
      </div>
    </div>
    <div class="login-right">
      <div class="form-wrapper">
        <div class="form-title">账户登录</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="邮箱"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on">
              <i slot="prefix" class="el-input__icon el-icon-message"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="至少 6 位"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
              <i class="toggle-eye" :class="passwordType === 'password' ? 'el-icon-view' : 'el-icon-view-off'" slot="suffix" @click="showPwd"></i>
            </el-input>
          </el-form-item>

          <div class="form-aux">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <a class="link-right" @click.prevent>忘记密码?</a>
          </div>

          <el-button :loading="loading" class="btn-login" type="primary" @click.native.prevent="handleLogin">
            登录
            <i class="el-icon-right"></i>
          </el-button>

          <div class="form-footer">
            <span>还没有账号?</span>
            <a class="link" @click.prevent="$router.push('/register')">联系开通</a>
          </div>

          <div class="back-home" @click="$router.push('/')">
            <i class="el-icon-back"></i>
            <span>返回首页</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
<!-- 页面整体结束 -->
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      rememberMe: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-form {
  .el-input__inner {
    height: 44px;
    border-radius: 10px;
    border-color: #e5e7eb;
  }
}
.toggle-eye { cursor: pointer; }
</style>

<style lang="scss" scoped>
$leftGradientStart: #6a11cb;
$leftGradientEnd: #2575fc;
$rightBg: #ffffff;
$textSubtle: #6b7280;
$titleColor: #111827;

.login-page { display: flex; min-height: 100vh; }

.login-left {
  flex: 1 1 50%;
  max-width: 50%;
  background: linear-gradient(135deg, $leftGradientStart 0%, $leftGradientEnd 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.left-inner { width: 420px; max-width: 86%; }

.hot-badge {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 16px;
}

.welcome {
  font-size: 36px;
  margin: 0 0 12px;
}

.desc {
  line-height: 1.8;
  opacity: 0.9;
  margin-bottom: 24px;
}

.feature {
  display: flex;
  align-items: center;
  background: rgba(255,255,255,0.15);
  border-radius: 10px;
  height: 48px;
  padding: 0 16px;
  margin-bottom: 14px;
  i { margin-right: 10px; }
}

.login-right {
  flex: 1 1 50%;
  max-width: 50%;
  background: $rightBg;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-wrapper { width: 420px; }

.form-title { font-size: 28px; color: $titleColor; font-weight: 700; margin-bottom: 28px; }

.form-aux { display: flex; align-items: center; justify-content: space-between; margin: 8px 0 16px; }
.link-right { color: #6366f1; cursor: pointer; }

.btn-login { width: 100%; height: 44px; background: linear-gradient(90deg, #5b8def 0%, #7b5bf2 100%); border: none; border-radius: 12px; box-shadow: 0 6px 14px rgba(99,102,241,0.25); }

.form-footer { color: $textSubtle; margin-top: 16px; .link { color: #6366f1; cursor: pointer; margin-left: 6px; } }

.back-home { margin-top: 16px; color: #6366f1; display: inline-flex; align-items: center; cursor: pointer; i { margin-right: 6px; } }
</style>
